package learningThread.syn.synchronizedMethodLockObj.communicate.producerAndConsumer;

import java.util.List;

public class Container {

    private List<Object> list;

    public Container(List<Object> list) {
        this.list = list;
    }

    public void add(Object obj){
        for (int i = 0; i < 100; i++) {
            synchronized (list){
                list.add(obj);
                System.out.println(Thread.currentThread().getName()+"......容器新增内容，当前容量:"+list.size());
                list.notify();
            }
        }



    }

    public void get(){
        for (int i = 0; i < 100; i++) {
            try {
                synchronized (list) {
                    if (list.size() == 0) {
                        System.out.println(Thread.currentThread().getName() + "......容器减少为0，消费者等待");
                        list.wait();
                    }
                    System.out.println(Thread.currentThread().getName() + "......容器减少内容");
                    Object remove = list.remove(0);
                    System.out.println("当前容量:" + list.size());
                    list.notify();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
